/**
 * Created by a1 on 16/9/23.
 */

function PageListCommentData() {
    this.Size = 10
    this.timeStamp = 0
    this.isLoading = false
    this.arrayData = new Array()
    this.nodeListContent
    this.nodeList
}

PageListCommentData.prototype.initNode = function() {
    this.nodeListContent = $("#dm_content_list_comment")
    this.nodeList = $("#dm_list_comment")
}

var pageListCommentData
function initListComment() {
    pageListCommentData = new PageListCommentData()
    $("#dm_content_list_comment_mask").hide()
}

function onShowListComment() {
    pageListCommentData.initNode()
    var nodeMask = $("#dm_content_list_comment_mask")
    var nodeFooter = $("#dm_footer_comment")
    var nodeInputContainer = $("#dm_comment_input_container")
    var nodeInput = $("#dm_comment_input")
    nodeMask.bind("click", function(){
        nodeInput.blur()
        nodeMask.hide()
        nodeMask.removeClass("edit")
        nodeFooter.removeClass("edit")
        nodeInputContainer.removeClass("edit")
    })
    nodeInput.bind("focus", function(){
        console.log("focus")
        nodeMask.show()
        nodeMask.addClass("edit")
        nodeFooter.addClass("edit")
        nodeInputContainer.addClass("edit")
    })
    if(pageListCommentData.arrayData.length == 0) {
        apiCommentList()
    }else {
        bindListCommentLoadMore()
    }
}

function bindListCommentLoadMore() {
    addLoadMore(pageListCommentData.nodeListContent)
    bindLoadMore($("#dm_page_list_comment"), function() {
        apiCommentList()
    })
}

function apiCommentList() {
    console.log("loadding")
    if(pageListCommentData.isLoading) {
        return
    }
    pageListCommentData.isLoading = true
    var url = "../test/data"
    $.getJSON(url, function(data){
        pageListCommentData.isLoading = false
        $.pullToRefreshDone(pageListCommentData.nodeListContent)
        var length = data.length
        console.log("lenght = " + length + " size = " + pageListCommentData.Size)
        if(length >= pageListCommentData.Size) {
            bindListCommentLoadMore()
        }else {
            removeLoadMore(pageListCommentData.nodeListContent)
        }
        pageListCommentData.arrayData = pageListCommentData.arrayData.concat(data)
        for(var i = 0; i < length; ++i) {
            var itemData = data[i]
            addItemComment(pageListCommentData.nodeList, itemData)
        }
        $.refreshScroller()
    })
}

function addItemComment(parent, data) {
    //console.log(index + " = " + JSON.stringify(data))
    var item = $("<div></div>")
    var content = $("<div class='dm_list_item_comment'></div>")
    var avatar = $("<img class='dm_list_item_comment_avatar' src='"+data[kImageUrl]+"'/>")
    item.append(avatar)
    var containerTop = $("<div class='dm_list_item_comment_top'></div>")
    var labelName = $("<div class='dm_list_item_comment_name'>"+data[kTitle]+"</div>")
    var labelDate = $("<div class='dm_list_item_comment_date'>"+data[kDate]+"</div>")
    containerTop.append(labelName)
    containerTop.append(labelDate)
    content.append(containerTop)
    var labelContent = $("<div class='dm_list_item_comment_content'>"+data[kContent]+"</div>")
    content.append(labelContent)

    var containerBottom = $("<div class='dm_list_item_comment_bottom'></div>")
    var imgUP = $("<img class='dm_list_item_comment_up_img' src='../image/activ2.gif'/>")
    var labelUp = $("<span class='dm_list_item_comment_up_count'>0</span>")
    containerBottom.append(imgUP)
    containerBottom.append(labelUp)
    content.append(containerBottom)
    var divider = "<div class='dm_divider'></div>"
    item.append(content)
    item.append(divider)
    parent.append(item)
    if(!imgUP.hasClass("up")) {
        containerBottom.click(function() {
            imgUP.addClass("up")
            imgUP.addClass("animation_scale")
            imgUP.attr("src", "../image/activ_2.gif")
            labelUp.addClass("up")
        })
    }
}